package com.example.demo.config.bigDecimalPattern;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class CustomBigDecimalSerializer extends JsonSerializer<BigDecimal> {

    private final int scale;

    public CustomBigDecimalSerializer(int scale) {
        this.scale = scale;
    }

    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        var valueStr= value.setScale(scale, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        gen.writeNumber(new BigDecimal(valueStr));
    }
}